package com.solidumgame.martillo.hammer
{
	import Box2D.Collision.Shapes.b2Shape;
	
	import com.pblabs.box2D.Box2DManagerComponent;
	import com.pblabs.box2D.Box2DSpatialComponent;
	import com.pblabs.box2D.PolygonCollisionShape;
	import com.pblabs.engine.PBE;
	import com.pblabs.engine.core.ObjectType;
	import com.solidumgame.martillo.tablewood.TableWood;
	import com.solidumgame.martillo.nail.Nail;
	
	import flash.geom.Point;
	
	public class HammerSpatialComponent extends Box2DSpatialComponent
	{
		public function HammerSpatialComponent()
		{
			super();
			
			this.spatialManager = PBE.lookupEntity("SpatialManager").lookupComponentByName("Manager") as Box2DManagerComponent;
			this.position = new Point(-220, 0);
			this.size = new Point(97, 52);
			//this.rotation = -5;
			this.canMove = false;
			this.canRotate = false;
			this.canSleep = false;
			this.collisionType = new ObjectType(Hammer.TYPE);
			this.collidesWithTypes = new ObjectType(Nail.TYPE,TableWood.TYPE);
			createHammer();
		}
		
		/**
		 * Se crea shape de head de Hammer.
		 * Se añade shape al spatialComponent. 
		 * 
		 */		
		private function createHammer():void{
			
			var ratioX:Number = 48.5;
			var ratioY:Number = 26;
			
			this.size = new Point(ratioX*2, ratioY*2);
			
			//Head
			
			var hammerHead:PolygonCollisionShape = new PolygonCollisionShape();
			hammerHead.density = 0;
			hammerHead.friction = 1;
			hammerHead.vertices = [new Point(17.5/ratioX, -26/ratioY),new Point(48.5/ratioX, -26/ratioY),new Point(48.5/ratioX, 26/ratioY),new Point(17.5/ratioX, 26/ratioY)];

			//Body
	
			var hammerBody:PolygonCollisionShape = new PolygonCollisionShape();
			hammerBody.density = 0;
			hammerBody.friction = 1;
			hammerBody.vertices = [new Point(-48.5/ratioX, -8/ratioY),new Point(17.5/ratioX, -8/ratioY),new Point(17.5/ratioX, 9/ratioY),new Point(-48.5/ratioX, 9/ratioY)];
			
			this.collisionShapes = new Array();
			//this.collisionShapes.push(hammerBody);
			this.collisionShapes.push(hammerHead);
		}
		
		override protected function onReset():void{
			
			super.onReset();
			var shape:b2Shape = this.body.m_shapeList;
			
			while(shape!=null){
				shape.m_isSensor = true;
				shape = shape.m_next;
			}
			
		}
	
	}
}